package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.service.AdminRoleService;
import com.cskaoyan.market.service.AdminRoleServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet("/admin/role/*")
public class AdminRoleController extends HttpServlet {
    AdminRoleService adminRoleService = new AdminRoleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath()+"/admin/role/", "");
        if("list".equals(op)){
            list(req,resp);
        }
    }
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name =req.getParameter("name");
        List<MarketRole> marketRole=adminRoleService.list(page,limit,sort,order,name);
        Object ok = ResponseUtil.okList(marketRole);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath()+"/admin/role/", "");
        if("create".equals(op)){
            create(req,resp);
        }
        if("update".equals(op)){
            update(req,resp);
        }
        if ("delete".equals(op)){
            delete(req,resp);
        }
    }
    private void create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonStr =req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketRole marketRole = objectMapper.readValue(jsonStr, MarketRole.class);
        marketRole.setAddTime(LocalDateTime.now());
        marketRole.setUpdateTime(LocalDateTime.now());
        adminRoleService.create(marketRole);
        Object ok = ResponseUtil.ok(marketRole);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonStr =req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketRole marketRole = objectMapper.readValue(jsonStr, MarketRole.class);
        marketRole.setUpdateTime(LocalDateTime.now());
        adminRoleService.update(marketRole);
        Object ok = ResponseUtil.ok();
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonStr =req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketRole marketRole = objectMapper.readValue(jsonStr, MarketRole.class);
        adminRoleService.delete(marketRole);
        Object ok = ResponseUtil.ok();
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);
    }
}
